require('Sound')

EventHandler = {}
EventHandler.__index = EventHandler

function love.keypressed(key)
     if key == "escape" then
          menu.option = #menu.view[menu.state] - 1
     elseif key == "up" then
          menu.option = (menu.option - 1) % #menu.view[menu.state]
     elseif key == "down" then
          menu.option = (menu.option + 1) % #menu.view[menu.state]
     elseif key == "return" or key == "enter" then
		if gameOver == false then
		  startTime = (os.time())
          menu:callback(menu.option)
		end
     end
end

function EventHandler:parseInput()
     if menu.run then
          -- Handled in love.keypressed()
          -- in order to disregard key repeats
     else
          --Get Input
         if love.keyboard.isDown( "up" ) then
               if self.currentPos[2] > 0 then
                    --Parse Up Key
                    for i, object in ipairs(self.objects) do
                         object.y = object.y + self.movDist
                    end
                    for i, star in ipairs(self.stars) do
                         star.y = star.y + self.movDist
                    end
                    for i=1, #self.staticStars do
                         self.staticStars[i][2] = self.staticStars[i][2] + self.movDist
                    end
                    for i, collectible in ipairs(self.collectibles) do
                         collectible.y = collectible.y + self.movDist
                    end

					  for i, parasite in ipairs(self.parasiteList) do
                         parasite.y = parasite.y + self.movDist
                    end

					  for i, blackHole in ipairs(self.blackHoles) do
						    blackHole.y = blackHole.y + self.movDist
					  end

                    self.glow.y = self.glow.y + self.movDist
                    self.currentPos[2] = self.currentPos[2] - self.movDist
               end
          end
         if love.keyboard.isDown( "down" ) then
               if self.currentPos[2] < self.maxY then
                    --Parse Down Key
                    for i, object in ipairs(self.objects) do
                         object.y = object.y - self.movDist
                    end
                    for i, star in ipairs(self.stars) do
                         star.y = star.y - self.movDist
                    end
                    for i=1, #self.staticStars do
                         self.staticStars[i][2] = self.staticStars[i][2] - self.movDist
                    end
                    for i, collectible in ipairs(self.collectibles) do
                         collectible.y = collectible.y - self.movDist
                    end

					  for i, parasite in ipairs(self.parasiteList) do
					        parasite.y = parasite.y - self.movDist
					  end

					  for i, blackHole in ipairs(self.blackHoles) do
						    blackHole.y = blackHole.y - self.movDist
					  end

                    self.glow.y = self.glow.y - self.movDist
                  self.currentPos[2] = self.currentPos[2] + self.movDist
               end
         end
         if love.keyboard.isDown( "left" ) then
               if self.currentPos[1] > 0 then
                    --Parse Left Key
                    for i, object in ipairs(self.objects) do
                         object.x = object.x + self.movDist
                    end
                    for i, star in ipairs(self.stars) do
                         star.x = star.x + self.movDist
                    end
                    for i=1, #self.staticStars do
                         self.staticStars[i][1] = self.staticStars[i][1] + self.movDist
                    end
                    for i, collectible in ipairs(self.collectibles) do
                         collectible.x = collectible.x + self.movDist
                    end

					  for i, parasite in ipairs(self.parasiteList) do
					        parasite.x = parasite.x + self.movDist
					  end

					  for i, blackHole in ipairs(self.blackHoles) do
						    blackHole.x = blackHole.x + self.movDist
					  end

                    self.glow.x = self.glow.x + self.movDist
                    self.currentPos[1] = self.currentPos[1] - self.movDist
               end
          end
         if love.keyboard.isDown( "right" ) then
               if self.currentPos[1] < self.maxX then
                    --Parse Right Key
                    for i, object in ipairs(self.objects) do
                         object.x = object.x - self.movDist
                    end
                    for i, star in ipairs(self.stars) do
                         star.x = star.x - self.movDist
                    end
                    for i=1, #self.staticStars do
                         self.staticStars[i][1] = self.staticStars[i][1] - self.movDist
                    end
                    for i, collectible in ipairs(self.collectibles) do
                         collectible.x = collectible.x - self.movDist
                    end
					   for i, parasite in ipairs(self.parasiteList) do
                         parasite.x = parasite.x - self.movDist
                    end
					  for i, blackHole in ipairs(self.blackHoles) do
						    blackHole.x = blackHole.x - self.movDist
					   end 

                    self.glow.x = self.glow.x - self.movDist
                    self.currentPos[1] = self.currentPos[1] + self.movDist
               end
         end
		 if love.keyboard.isDown( " " ) then
			if self.player.currentPulseCD == 0 then
                    Sound:pulse()
				self.player.currentPulseCD = self.player.maxPulseCD
				self.player.isPulseActive = true
				self.player.pulseTimer = 72
				self.player.health = self.player.health - 200
			end
		end
		--TESTING FEATURE
		if love.keyboard.isDown( "home" ) then
			self.player.health = 9999999
		end
     end
end

function EventHandler.load(world)
     local eventHandler = {}
	setmetatable(eventHandler, EventHandler)
	eventHandler.player = world.player
	eventHandler.maxX = world.width
	eventHandler.maxY = world.height
	eventHandler.currentPos = {world.width / 2, world.height / 2}
	eventHandler.objects = world.swirlyList
	eventHandler.stars = world.starList
	eventHandler.collectibles = world.collectibleList
	eventHandler.parasiteList = world.parasiteList
	eventHandler.staticStars = world.staticStarList
	eventHandler.blackHoles = world.blackHoleList
	eventHandler.glow = world.glow
	eventHandler.firstpop = true
	eventHandler.keyt = 0
	eventHandler.movDist = 5
     eventHandler.openingMovTot = 0
	return eventHandler
end

function EventHandler:update(dt, world)
	self.player = world.player
	self.objects = world.swirlyList
	self.collectibles = world.collectibleList
	self.parasiteList = world.parasiteList
	self.glow = world.glow
	self:parseInput()
end	

function EventHandler:draw()
end

function EventHandler:opening()
     local openingMov = 1
     
     if self.openingMovTot > 25 then
          openingMov = 1.5
     elseif self.openingMovTot > 50 then
          openingMov = 2
     elseif self.openingMovTot > 80 then
          openingMov = 2.5
     end
     
     if self.currentPos[2] > 0 then
          for i, object in ipairs(self.objects) do
               object.y = object.y + openingMov
          end
          for i, star in ipairs(self.stars) do
               star.y = star.y + openingMov
          end
          for i=1, #self.staticStars do
               self.staticStars[i][2] = self.staticStars[i][2] + openingMov
          end
          for i, collectible in ipairs(self.collectibles) do
               collectible.y = collectible.y + openingMov
          end

                 for i, parasite in ipairs(self.parasiteList) do
               parasite.y = parasite.y + openingMov
          end

                 for i, blackHole in ipairs(self.blackHoles) do
                        blackHole.y = blackHole.y + openingMov
                 end

          self.glow.y = self.glow.y + openingMov
          self.currentPos[2] = self.currentPos[2] - openingMov
     end
     
     self.openingMovTot = self.openingMovTot + openingMov
     if self.openingMovTot > 200 then
          playerControl = true
          tutClock = love.timer.getTime()
     end
end